package com.cal.utms.action.user_maintenance;

import com.cal.utms.dao.user_maintenance.UserDaoImpl;
import com.cal.utms.dto.user_maintenance.UserDto;
import com.cal.utms.model.user_maintenance.UserModel;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven<UserDto> {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private UserDto userDto = new UserDto();
	UserDaoImpl daoImpl = new UserDaoImpl();
	UserModel userModel = new UserModel();
	private String user_name;
	private int user_id;

	public String updateUserObject() {
		userDto.setId(user_id);
		userDto.setSuccess(true);
		daoImpl.updateUser(UserModel.createUserModelWithId(userDto));
		return Action.SUCCESS;
	}

	public String saveUser() {
		userModel.saveUser(userDto);
		return Action.SUCCESS;
	}

	public String getUserName() {
		user_name = user_name.replaceAll("^\"|\"$", "");
		userDto.setSuccess(UserModel.createUserDtoModle(daoImpl.getUserByUsername(user_name.toUpperCase())));
		return Action.SUCCESS;
	}

	public String getAllUsers() {
		userDto.setUserDtos(UserModel.createUserDtoList(daoImpl.getAllUsers()));
		userDto.setSuccess(true);
		return Action.SUCCESS;
	}

	public String checkUserName() {

		return ActionSupport.SUCCESS;
	}

	public UserDto getUserDto() {
		return userDto;
	}

	public void setUserDto(UserDto userDto) {
		this.userDto = userDto;
	}

	@Override
	public UserDto getModel() {
		return userDto;
	}

	public String getUser_name() {
		return user_name;
	}

	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}

	public int getUser_id() {
		return user_id;
	}

	public void setUser_id(int user_id) {
		this.user_id = user_id;
	}

}
